import {
  reqAsk
} from '../../utils/api.js'

import {
  getReqData
} from '../../utils/util.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    arr: [],
    loading: true,
    hideCard: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    wx.showLoading({
      title: '请稍后...',
    })

    const {
      result
    } = await reqAsk()
    const {
      data: arr,
      phone
    } = getReqData(result)

    if (!arr) return null

    this.setData({
      arr,
      phone,
      loading: false,
      hideCard: true
    })

    wx.hideLoading()
  },
  /**
   * 在线咨询
   */
  webAsk: function (e) {
    const {
      index
    } = e.currentTarget.dataset

    const newInfo = this.data.arr[index]

    wx.navigateTo({
      url: '/pages/ask_detail/ask_detail',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromAskPage', {
          ...newInfo
        })
      }
    })
  },
  /**
   * 电话咨询
   */
  telephoneAsk: function () {
    const {
      phone
    } = this.data

    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
})